package com.tinysand.advert.controller;

import com.alibaba.fastjson.JSON;
import com.tinysand.advert.entity.AdvertPlan;
import com.tinysand.advert.exception.AdvertException;
import com.tinysand.advert.service.AdPlanService;
import com.tinysand.advert.support.AdPlanGetRequest;
import com.tinysand.advert.support.AdPlanRequest;
import com.tinysand.advert.support.AdPlanResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 推广计划操作控制器
 *
 * Created By TINY
 * Create Time 2020/2/22
 **/
@Slf4j
@RestController
public class PlanController {

    private final AdPlanService adPlanService;

    public PlanController(AdPlanService adPlanService) {
        this.adPlanService = adPlanService;
    }

    @PostMapping("/plan/create")
    public AdPlanResponse createAdPlan(
            @RequestBody AdPlanRequest request) throws AdvertException {
        log.info("创建推广计划，请求参数（{}）", JSON.toJSONString(
                request
        ));

        return adPlanService.createAdPlan(request);
    }

    @PostMapping("/plan/get")
    public List<AdvertPlan> getAdPlanByIds(
            @RequestBody AdPlanGetRequest request) throws AdvertException {
        log.info("获取推广计划，请求参数（{}）", JSON.toJSONString(
                request
        ));

        return adPlanService.getAdPlanByIds(request);
    }

    @PutMapping("/plan/update")
    public AdPlanResponse updateAdPlan(
            @RequestBody AdPlanRequest request) throws AdvertException {
        log.info("更新推广计划，请求参数（{}）", JSON.toJSONString(
                request
        ));

        return adPlanService.updateAdPlan(request);
    }

    @DeleteMapping("/plan/delete")
    public void deleteAdPlan(
            @RequestBody AdPlanRequest request) throws AdvertException {
        log.info("删除推广计划，请求参数（{}）", JSON.toJSONString(
                request
        ));

        adPlanService.deleteAdPlan(request);
    }
}
